/*
 * 
 * EVENT CONTROLLER PLOX !!!!
 * 
 * 
 * */
 package ga.wavaplayer.ui {

	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import ga.wavaplayer.events.WEvent;
	import ga.wemanager.WEManager;
		
	public class WSeekBar extends Sprite {
		
		private var _config:Object;
		private var _colors:Object;

		private var _lineColor:uint;
		private var _barColor:uint;
		private var _barStart:Number = 0;
		
		private var _barContainer:Shape = new Shape();
		private var _bar:Shape = new Shape();
		private var _loadedBar:Shape = new Shape();
		
		private var _barWidth:Number = 60;
		
		private var _currentTime:Number = 0;
		private var _timeLoadedRatio:Number = 0;
		private var _timeTotal:Number = 0;
		private var _timeCurrent:Number = 0;
		
		public function WSeekBar(pConfig:Object, pColors:Object) {
			_config = pConfig;
			_colors = pColors;
			
			_lineColor = _colors.semilight;
			_barColor = _colors.highlight.semilight;
			
			this.buttonMode = true;
			
			this.graphics.beginFill(0xff0000, 0);
			this.graphics.drawRect(0, 0, _config.height * 3, _config.height);
			this.graphics.endFill();
			
			_barContainer.y = Math.floor(_config.height / 3);
			
			this._drawLine();
			
			this.addEventListener(MouseEvent.MOUSE_OVER, _mouseOverHandler);
			this.addEventListener(MouseEvent.MOUSE_OUT, _mouseOutHandler);
			this.addEventListener(MouseEvent.CLICK, _clickHandler);
			
			_bar.x = _barContainer.x + 1;
			_bar.y = _barContainer.y + 1;
			_loadedBar.x = _bar.x;
			_loadedBar.y = _bar.y;
			
			this.addChild(_barContainer);
			this.addChild(_loadedBar);
			this.addChild(_bar);
		}
		
		public function updateProgress():void{
			
		}
		
		private function _drawLine():void {
			_barContainer.graphics.clear();
			_barContainer.graphics.beginFill(_lineColor);
			_barContainer.graphics.drawRect(0, 0, _barWidth, _config.height - (_barContainer.y * 2));
			_barContainer.graphics.beginFill(_colors.dark);
			_barContainer.graphics.drawRect(1, 1, _barWidth - 2, _config.height - (_barContainer.y * 2) - 2);
			_barContainer.graphics.endFill();
		}
		
		private function _drawCurrentBar():void {
			if(_timeCurrent > 0 && _timeTotal > 0){
				_bar.graphics.clear();
				_bar.graphics.beginFill(_barColor);
				_bar.graphics.drawRect(_barStart, 0, (_barContainer.width - 2 - _barStart) * (_timeCurrent / _timeTotal), _barContainer.height - 2);
				_bar.graphics.endFill();
			}
		}
		
		private function _drawLoadedBar():void {
			_loadedBar.graphics.clear();
			_loadedBar.graphics.beginFill(_colors.highlight.semidark);
			_loadedBar.graphics.drawRect(_barStart, 0, (_barContainer.width - 2 - _barStart) * _timeLoadedRatio, _barContainer.height - 2);
			_loadedBar.graphics.endFill();
		}
		
		private function _mouseOverHandler(e:MouseEvent):void {
			_lineColor = _colors.light;
			_barColor = _colors.highlight.light;
			this._drawLine();
			this._drawLoadedBar();
			this._drawCurrentBar();
		}
		
		private function _mouseOutHandler(e:MouseEvent):void {
			_lineColor = _colors.semilight;
			_barColor = _colors.highlight.semilight;
			this._drawLine();
			this._drawLoadedBar();
			this._drawCurrentBar();
		}
		
		private function _clickHandler(e:MouseEvent):void {
			var event:WEvent = new WEvent(WControls.SEEK_REQUEST);
			event.ratio = (e.target.mouseX - 1) / (e.target.width - 2);
			
//			_barStart = Math.round((_barContainer.width - 2) * event.ratio);
			
			WEManager.dispatchEvent(event);
		}
		
		public function set barWidth(pValue:Number):void{
			_barWidth = pValue;
			this._drawLine();
			this._drawLoadedBar();
			this._drawCurrentBar();
		}
		
		public function set timeLoadedRatio(pValue:Number):void {
			_timeLoadedRatio = pValue;
			this._drawLoadedBar();
		}
		
		public function set timeTotal(pValue:Number):void{
			_timeTotal = pValue;
			this._drawLoadedBar();
			this._drawCurrentBar();
		}
		
		public function set timeCurrent(pValue:Number):void {
			_timeCurrent = pValue;
			this._drawCurrentBar();
		}
	}
}
